import cv2
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.text import MIMEText
from smtplib import SMTP_SSL
from email.mime.image import MIMEImage


def get_photo():                         #开启摄像头并拍照
    cap = cv2.VideoCapture(0)              #开启摄像头，0表示默认摄像头
    f, frame = cap.read()                   #获取摄像头所拍摄的内容
    cv2.imwrite('image.jpg', frame)       #保存为jpg图片格式文件
    cap.release()                        #关闭摄像头

def send_message():                      #设置邮箱参数
    host_server = 'smtp.qq.com'            #邮件服务
    pwd = '******************'               #填入smtp的授权码或者密码
    from_qq_mail='**************'         #填写发件邮箱
    to_qq_mail = '**************'         #填写收件邮箱

    with open('image.jpg', 'rb')as fp:
        image = MIMEImage(fp.read())
        image['Content-Type'] = 'application/octet-stream'
        image['Content-Disposition'] = 'attachment;filename="image.jpg"'

    msg = MIMEMultipart()                  #创建邮件格式
    msg['Subject'] = Header('开机照片', 'UTF-8')            #邮件主题
    msg['From'] = from_qq_mail                            #发件人
    msg['To'] = to_qq_mail                              #收件人
    msg.attach(MIMEText('开机照片', 'html', 'UTF-8'))      #邮件内容
    msg.attach(image)                                    #邮件附件

    smtp = SMTP_SSL(host_server)                                  #连接邮箱
    smtp.login(from_qq_mail, pwd)
    smtp.sendmail(from_qq_mail, to_qq_mail, msg.as_string())
    smtp.quit()

if __name__=='__main__':
    get_photo()
    send_message()